package cn.edu.suda.catalina.health;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MenuFragment.OnMenuFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MenuFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MenuFragment extends Fragment {

    private OnMenuFragmentInteractionListener mListener;

    public MenuFragment() {}

    public static MenuFragment newInstance() {
        return new MenuFragment();
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.menu_one:
                    startActivity(new Intent(getActivity(), RiskActivity.class));
                    break;
                case R.id.menu_two:
                    onButtonPressed(NutritionFragment.newInstance(), null);
                    break;
                case R.id.menu_three:
                    onButtonPressed(SportsFragment.newInstance(), null);
                    break;
                case R.id.menu_four:
                    onButtonPressed(GameFragment.newInstance(), null);
                    break;
                case R.id.menu_five:
                    startActivity(new Intent(getActivity(), NewsActivity.class));
                    break;
                case R.id.menu_six:
                    onButtonPressed(AnotherFragment.newInstance(), AnotherFragment.FRAGMENT_TAG);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_menu, container, false);

        rootView.findViewById(R.id.menu_one).setOnClickListener(mClickListener);
        rootView.findViewById(R.id.menu_two).setOnClickListener(mClickListener);
        rootView.findViewById(R.id.menu_three).setOnClickListener(mClickListener);
        rootView.findViewById(R.id.menu_four).setOnClickListener(mClickListener);
        rootView.findViewById(R.id.menu_five).setOnClickListener(mClickListener);
        rootView.findViewById(R.id.menu_six).setOnClickListener(mClickListener);

        return rootView;
    }

    public void onButtonPressed(Fragment fragment, String tag) {
        if (mListener != null) {
            mListener.onMenuFragmentInteraction(fragment, tag);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnMenuFragmentInteractionListener) {
            mListener = (OnMenuFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnMenuFragmentInteractionListener {
        void onMenuFragmentInteraction(Fragment fragment, String tag);
    }
}
